昨天建立好 Sinopac controller,今天持續完成 receive_msg的部分
增加 Sinopac controller 的函數,要求 API 需要的一些參數
// app/Http/Controllers/Sinopac.php
...
public function requestDataset(string $service_name, array $dataset)
{
$nonce = $this->getNonce();
$hash_id = $this->calcHashId();
$iv = $this->calculateIv($nonce);
$sign = $this->generateSign($dataset, $nonce, $hash_id);
$message = $this->encryptMessage($dataset, $hash_id, $iv);
return [
'Version' => '1.0.0',
'ShopNo' => $this->shop_no,
'APIService' => $service_name,
'Nonce' => $nonce,
'Sign' => $sign,
'Message' => $message
];
}
...
在 Controller 建立 Sinopac 的初始化函數,有使用到的函數再初始化就好了,所以不用放在 __construct
商店代碼
跟雜湊值(key)
// app/Http/Controllers/Controller.php
...
private function initSinopac()
{
return new Sinopac(
'Shop_no',
'key_a1',
'key_a2',
'key_b1',
'key_b2');
}
...
同樣修改 Controller 的 receive_msg 函數,確保好 Debug,每次要求都先記 log
// app/Http/Controllers/Controller.php
...
public function receive_msg(Request $request)
{
Log::alert('Receive message Content', $request->all());
$PayToken = $request->get('PayToken');
if (!$PayToken) {
Log::alert('PayToken Not exist');
return ['Status' => 'F'];
}
$sinopac = $this->initSinopac();
$data = $sinopac->requestDataset('OrderPayQuery', $request->all());
$message = $sinopac->callApi('https://apisbx.sinopac.com/funBIZ/QPay.WebAPI/api/Order', $data);
Log::info('Reply message', (array) $message);
return ['Status' => 'S'];
}
...
使用 postman 試看看正不正常,並用 powershell 監看 Log
> Get-Content -Wait -Tail 10 -Encoding "utf8" .\storage\logs
Postman:
Log:
是的,崩潰的一天,一開始測試都正常,整個完成後直接報錯爆好爆滿...
明天先回頭嘗試建立訂單吧